package com.myblog.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.DeleteById;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.myblog.Utils.BeanCopyUtils;
import com.myblog.domain.GlobalResponse;
import com.myblog.domain.Vo.PageVo;
import com.myblog.domain.Vo.TagVo;
import com.myblog.domain.entity.Tag;
import com.myblog.domain.entity.enums.GlobalResponseEnum;
import com.myblog.expection.GlobalException;
import com.myblog.services.TagService;
import com.myblog.mapper.TagMapper;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
* @author Administrator
* @description 针对表【sg_tag(标签)】的数据库操作Service实现
* @createDate 2022-10-20 16:14:59
*/
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag>
implements TagService{

    @Override
    public GlobalResponse getList(Integer pageNum, Integer pageSize,Tag tag) {
        LambdaQueryWrapper<Tag> lambdaQueryWrapper=new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(StringUtils.hasText(tag.getName()),Tag::getName,tag.getName());
        lambdaQueryWrapper.eq(StringUtils.hasText(tag.getRemark()),Tag::getRemark,tag.getRemark());

        Page page=new Page(pageNum,pageSize);
        page(page, lambdaQueryWrapper);

        List<TagVo> rows = BeanCopyUtils.copyBeanList(page.getRecords(), TagVo.class);
        PageVo pageVo = new PageVo(rows, page.getTotal());
        return GlobalResponse.ok(pageVo);
    }



    @Override
    public GlobalResponse add(Tag tag) {
        save(tag);
        return GlobalResponse.ok();
    }

    @Override
    public GlobalResponse deleteTag(Long id) {
        if(Objects.isNull(getById(id))){
            throw new GlobalException(GlobalResponseEnum.ID_NO_EXIST);
        }
        removeById(id);
        return GlobalResponse.ok();
    }

    @Override
    public GlobalResponse getTag(Long id) {
        Tag tag=getById(id);
        if(Objects.isNull(getById(id))){
            throw new GlobalException(GlobalResponseEnum.ID_NO_EXIST);
        }
        TagVo tagVo=BeanCopyUtils.copyBean(tag,TagVo.class);
        return GlobalResponse.ok(tagVo);
    }

    @Override
    public GlobalResponse updateTag(Tag tag) {

        if(Objects.isNull(tag.getId())){
            throw new GlobalException(GlobalResponseEnum.NO_DATA);
        }
        if (Objects.isNull(getById(tag.getId()))){
            throw  new GlobalException(GlobalResponseEnum.ID_NO_EXIST);
        }
        updateById(tag);
        return GlobalResponse.ok();
    }

    @Override
    public GlobalResponse getAllTag() {
        LambdaQueryWrapper<Tag> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Tag::getName,Tag::getId);
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(list(lambdaQueryWrapper), TagVo.class);
        return GlobalResponse.ok(tagVos);
    }
}
